<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" :title="title" :width="width" :visible="visible" @ok="handleOk" @cancel="handleCancel" @close="handleClose" destroyOnClose>
    <CaseManageForm ref="registerForm" @ok="submitCallback" :formDisabled="disableSubmit" :formBpm="false"></CaseManageForm>
  </BasicDrawer>
</template>

<script lang="ts" setup>
import {computed, defineComponent, defineExpose, nextTick, ref, unref} from 'vue';
import {BasicDrawer, useDrawerInner} from '/@/components/Drawer';
import CaseManageForm from './CaseManageForm.vue'

const emit = defineEmits(['register', 'success']);
const title = ref<string>('');
const width = ref<number>(1200);
const visible = ref<boolean>(false);
const disableSubmit = ref<boolean>(false);
const registerForm = ref();
const isUpdate = ref(true);

const [registerDrawer,{ closeDrawer }] = useDrawerInner((data) => {
  // 方式1
  console.log('useDrawerInner:'+data.isUpdate)
  isUpdate.value = !!data?.isUpdate;
  disableSubmit.value = data.disableSubmit;
  if (unref(isUpdate)) {
    edit(data.record);
  } else{
    add();
  }
});
/**
 * 新增
 */
function add() {
  title.value = '新增';
  visible.value = true;
  nextTick(() => {
    registerForm.value.add();
  });
}

/**
 * 编辑
 * @param record
 */
function edit(record) {
  title.value = disableSubmit.value ? '详情' : '编辑';
  visible.value = true;
  nextTick(() => {
    registerForm.value.edit(record);
  });
}

/**
 * 确定按钮点击事件
 */
function handleOk() {
  registerForm.value.submitForm();
}

/**
 * form保存回调事件
 */
function submitCallback() {
  handleCancel();
  emit('success');
}

/**
 * 取消按钮回调事件
 */
function handleCancel() {
  visible.value = false;
}

defineExpose({
  add,
  edit,
  disableSubmit,
});
</script>

<style>
/**隐藏样式-modal确定按钮 */
.jee-hidden {
  display: none !important;
}
</style>
